package top.leyi.handle;

import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.leyi.IService.IActivityService;
import top.leyi.entity.activity.Activity;
import top.leyi.entity.user.User;
import top.leyi.utils.Result;
import top.leyi.utils.ResultGenerator;

@RestController
@RequestMapping("/api/activity")
public class ActivityHandle {

    @Autowired
    IActivityService service;

    /**
     * @desc 添加活动
     * @param token
     * @param name
     * @param place
     * @param descripe
     * @param pics
     * @param userId
     * @return
     */
    @PostMapping("")
    public Result addActivity(@RequestParam("token") String token,
                              @RequestParam("name") String name,
                              @RequestParam("place") String place,
                              @RequestParam("descripe") String descripe,
                              @RequestParam("pics") String pics,
                              @RequestParam("userId") int userId
                               ){

        Activity activity = new Activity();
        activity.setName(name);
        activity.setPlace(place);
        activity.setDescripe(descripe);
        activity.setPics(pics);

        User user = new User();
        user.setToken(token);
        user.setId(userId);

        service.addActivity(activity, user);
        return ResultGenerator.OK.getResult();
    }

    /**
     * 更新活动
     * @param token
     * @param name
     * @param place
     * @param descripe
     * @param pics
     * @param userId
     * @return
     */
    @PutMapping("")
    public Result updateActivity(@RequestParam("token") String token,
                                @RequestParam("name") String name,
                                @RequestParam("place") String place,
                                @RequestParam("descripe") String descripe,
                                @RequestParam("pics") String pics,
                                @RequestParam("userId") int userId,
                                @RequestParam("id") int id
    ){

        Activity activity = new Activity();
        activity.setName(name);
        activity.setPlace(place);
        activity.setDescripe(descripe);
        activity.setPics(pics);
        activity.setId(id);


        User user = new User();
        user.setToken(token);
        user.setId(userId);

        service.updateActivity(activity, user);
        return ResultGenerator.OK.getResult();
    }

    /**
     * 删除活动
     * @param token
     * @param name
     * @param place
     * @param descripe
     * @param pics
     * @param userId
     * @return
     */
    @DeleteMapping("")
    public Result deleteActivity(@RequestParam("token") String token,
                                 @RequestParam("id") int id,
                                     @RequestParam("userId") int userId
    ){

        Activity activity = new Activity();
        activity.setId(id);
        User user = new User();
        user.setToken(token);
        user.setId(userId);

        service.deleteActivity(activity, user);
        return ResultGenerator.OK.getResult();
    }


    /**
     * @desc 查看当前页面活动
     * @param page
     * @param token
     * @return
     */
    @GetMapping("/{page}")
    public Result getActivity(
            @PathVariable(value = "page",required =  false) Integer page,
            @RequestParam("token") String token){

        if(page == null){
            page = 1;
        }


        return  new Result(200, service.getSimpleActivity(page));
    }
    /**
     * @desc 查看详细完整页面
     * @param page
     * @param token
     * @return
     */
    @GetMapping("/v/{page}")
    public Result getDetActivity(
            @PathVariable(value = "page",required =  false) Integer page,
            @RequestParam("token") String token){

        if(page == null){
            page = 1;
        }


        return  new Result(200, service.getDetActivity(page));
    }
    /**
     * @desc 查看活动细节
     * @param id
     * @param userId
     * @param token
     * @return
     */
    @GetMapping("/detail")
    public Result getActivityDetail(
            @RequestParam(value = "id") Integer id,
            @RequestParam(value = "userId") Integer userId,
            @RequestParam("token") String token){


        User user = new User();
        user.setToken(token);
        user.setId(userId);

        return  new Result(200, service.getActivity(id, user));
    }

    /**
     * @desc 加入或者取消加入一个社团活动
     * @param id
     * @param userId
     * @param token
     * @return
     */
    @PutMapping ("/join")
    public Result joinOrCancelOneActivity(
            @RequestParam(value = "id") Integer id,
            @RequestParam(value = "userId") Integer userId,
            @RequestParam("token") String token){

        User user = new User();
        user.setToken(token);
        user.setId(userId);

        return new Result(200, service.joinOrCancelOneActivity(id, user));
    }
}
